package demo.swing.passenger;
import demo.swing.Flight;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;

import demo.swing.SqlQuery;
import demo.swing.tickets.SelectTickets;

public class EnterPassengerListener implements ActionListener {

	private EnterPassenger enterPassenger = null;
	private Flight flight = null;
	private String name = null;
	private String passport = null;
	private String surname = null;
	private String patronymic = null;
	private String nationality = null;
	private String passengerId = null;
	private String flightId = null;
	private Integer priceBisnesTicketWithBonus = null;
	private Integer priceBisnesTicketWithoutBonus = null;
	private Integer priceEconomTicketWithBonus = null;
	private Integer priceEconomTicketWithoutBonus = null;
	public String getFlightId() {
		return flightId;
	}
	private String bonus = null;
	private SqlQuery sql = null;
	private Integer countID = null;
	private Integer countAllID = null;
	private Integer parseMany(String s){
		Integer res = null;
		String temp = s.substring(3);
		String  price = "";
		temp = temp.substring(0, temp.indexOf("."));
		char[] tmp = temp.toCharArray();
		for (int i = 0; i < temp.length(); i++){
			if (tmp[i] >= '0' && tmp[i] <= '9'){
				price += (tmp[i]);
			}
		}
		res = Integer.valueOf(price);
		return res;
	}
	private Integer getPrice(boolean isBisnes, boolean isBonus){
		Integer res = null;
		ResultSet set = null;
		String query = null;
		Integer bonus = Integer.valueOf(getBonus());
		if(isBisnes){
				//bisnes not bonus
				query = "select f.price_b " +
						"from flight f " +
						"where f.id = '" + getFlightId() + "';";
				try {
					set = sql.executeSqlQuery(query);
					while(set.next()){
						res = parseMany(set.getString(1));
						if (isBonus){
							if (bonus > res){
								res = new Integer(0);
							}else{
								res -= bonus;
							}
						}
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					return res;
				}						
		}else{
			//econom
			//bisnes not bonus
			query = "select f.price_e " +
					"from flight f " +
					"where f.id = '" + getFlightId() + "';";
			try {
				set = sql.executeSqlQuery(query);
				while(set.next()){
					res = parseMany(set.getString(1));
					if (isBonus){
						if (bonus > res){
							res = new Integer(0);
						}else{
							res -= bonus;
						}
					}
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return res;
			}						
			
		}
		return res;
	}
	public EnterPassengerListener(EnterPassenger p, Flight f) {
		// TODO Auto-generated constructor stub
		enterPassenger = p;
		flight = f;
		try {
			sql = new SqlQuery("192.168.1.4", "5432", "pgadmin", "Vyrdalak");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	private String pricesToString(){
		String res = "";
		res += "[Bisnes class with bonus] Price of ticket:" + priceBisnesTicketWithBonus + "\n";
		res += "[Bisnes class without bonus] Price of ticket:" + priceBisnesTicketWithoutBonus + "\n";
		res += "[Econom class with bonus] Price of ticket: " + priceEconomTicketWithBonus + "\n";
		res += "[Econom class without bonus] Pricew of ticket:" + priceEconomTicketWithoutBonus + "\n";
		return res;
	}
	public SqlQuery getSql(){
		return sql;
	}
	public String toString(){
		return new String(getFlight().toString() + getEnterPassenger().toString() + "The number of bonus points: " + bonus + "\n" + pricesToString());
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if (e.getSource() == enterPassenger.getOkBtn()){
			name = enterPassenger.getNameValue().getText();
			surname = enterPassenger.getSurname().getText();
			patronymic = enterPassenger.getPatronymic().getText();
			passport = enterPassenger.getPassport().getText();
			nationality = enterPassenger.getNationality().getText();
			System.out.println(enterPassenger.toString() + flight.toString());
			enterPassenger.setVisible(false);
			String query = "select COUNT(p.id) from passenger p " +
			"where p.passport = '" + passport + "';";
			System.out.println(query);
			try {
				ResultSet res = sql.executeSqlQuery(query);
				while(res.next()){
					countID = Integer.valueOf(res.getString(1));
					System.out.println(countID);
				}
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			if (countID == 0){
				//adding new Passenger
				query = "select COUNT(*) FROM passenger";
				System.out.println(query);
				try {
					ResultSet res = sql.executeSqlQuery(query);
					while(res.next()){
						countAllID = Integer.valueOf(res.getString(1));
						System.out.println("countAllID " + countAllID);
					}
					
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				String allName = "";
				if (name != null){
					allName += name + " ";
				}
				if (surname != null){
					allName += surname + " ";
				}
				if (patronymic != null){
					allName += patronymic;
				}
				countAllID++;
				query = "insert into passenger values ('" +
				countAllID +
				"', '" +
				allName +
				"', '" +
				passport +
				"', '" +
				nationality +
				"', '0');";
				System.out.println(query);
				try {
					sql.executeSqlUpdate(query);
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			query = "select p.id, p.bonus from passenger p where p.passport = '" + passport + "';";
			ResultSet res;
			try {
				res = sql.executeSqlQuery(query);
				while(res.next()){
					passengerId = res.getString(1);
					bonus = res.getString(2);
				}
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			query = "select r.id from flight r where r.num_flight = '" + flight.getFlightNum() + "';";
			try {
				res = sql.executeSqlQuery(query);
				while(res.next()){
					flightId = res.getString(1);
				}
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			priceBisnesTicketWithBonus = getPrice(true, true);
			priceBisnesTicketWithoutBonus = getPrice(true, false);
			priceEconomTicketWithBonus = getPrice(false, true);
			priceEconomTicketWithoutBonus = getPrice(false, false);
			SelectTickets selectTickets = new SelectTickets("Select type of ticket!", this);
		}

	}
	public Integer getPriceBisnesTicketWithBonus() {
		return priceBisnesTicketWithBonus;
	}
	public Integer getPriceBisnesTicketWithoutBonus() {
		return priceBisnesTicketWithoutBonus;
	}
	public Integer getPriceEconomTicketWithBonus() {
		return priceEconomTicketWithBonus;
	}
	public Integer getPriceEconomTicketWithoutBonus() {
		return priceEconomTicketWithoutBonus;
	}
	
	public String getBonus(){
		return bonus;
	}
	
	public String getPassengerId(){
		return passengerId;
	}
	public EnterPassenger getEnterPassenger() {
		return enterPassenger;
	}
	public Flight getFlight() {
		return flight;
	}

}
